package com.bill99.golden.inf.sso.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class CM {
	private static String base64 = "";
	static {
		StringBuffer sb = new StringBuffer(200);
		for (char c = 'A'; c <= 'Z'; c++) {
			sb.append(c);
		}
		for (char c = 'a'; c <= 'z'; c++) {
			sb.append(c);
		}
		for (char c = '0'; c <= '9'; c++) {
			sb.append(c);
		}
		sb.append('\n');
		sb.append('\r');
		sb.append('\t');
		sb.append(' ');
		sb.append('+');
		sb.append('/');
		sb.append('=');
		sb.append("-");
		CM.base64 = sb.toString();
	}

	public static int toInt(Object str) {
		if (str == null) {
			return 0;
		}
		if (str instanceof Integer) {
			Integer n = (Integer) str;
			return n.intValue();
		} else if (str instanceof Long) {
			Long n = (Long) str;
			return n.intValue();
		} else if (str instanceof Double) {
			Double n = (Double) str;
			return n.intValue();
		} else if (str instanceof java.math.BigDecimal) {
			BigDecimal n = (BigDecimal) str;
			return n.intValue();
		}
		try {
			return Integer.parseInt(str.toString().trim());
		} catch (Exception e) {
			return 0;
		}
	}

	public static String toString(Object o) {
		if (o == null) {
			return "";
		}
		return o.toString();
	}

	public static String toString(Object o, String defaultStr) {
		if (o == null) {
			return defaultStr;
		}
		String str = o.toString();
		if (str.trim().equals(""))
			return defaultStr;
		return str;
	}

	public static String toString(String msg, Throwable e) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PrintWriter pw = new PrintWriter(baos);
		pw.println(msg);
		e.printStackTrace(pw);
		pw.flush();
		String errMsg = new String(baos.toByteArray());
		return errMsg;
	}

	public static long toLong(Object str) {
		if (str == null) {
			return 0;
		}
		if (str instanceof Integer) {
			Integer n = (Integer) str;
			return n.longValue();
		} else if (str instanceof Long) {
			Long n = (Long) str;
			return n.longValue();
		} else if (str instanceof Double) {
			Double n = (Double) str;
			return n.longValue();
		} else if (str instanceof java.math.BigDecimal) {
			BigDecimal n = (BigDecimal) str;
			return n.longValue();
		}
		try {
			return Long.parseLong(str.toString().trim());
		} catch (Exception e) {
			return 0;
		}
	}

	public static String convertUnicode2UTF8Byte(String instr) {
		if (instr == null)
			return "";
		int len = instr.length();
		byte[] abyte = new byte[len << 2];
		int j = 0;
		String str = "";
		for (int i = 0; i < len; i++) {
			char c = instr.charAt(i);
			if (c < 0x80) {
				abyte[j++] = (byte) c;
				str += c;
			} else if (c < 0x0800) {
				abyte[j++] = (byte) (((c >> 6) & 0x1F) | 0xC0);
				str += "%" + Integer.toHexString(abyte[j - 1]).substring(6);
				abyte[j++] = (byte) ((c & 0x3F) | 0x80);
				str += "%" + Integer.toHexString(abyte[j - 1]).substring(6);
			} else if (c < 0x010000) {
				abyte[j++] = (byte) (((c >> 12) & 0x0F) | 0xE0);
				str += "%" + Integer.toHexString(abyte[j - 1]).substring(6);
				abyte[j++] = (byte) (((c >> 6) & 0x3F) | 0x80);
				str += "%" + Integer.toHexString(abyte[j - 1]).substring(6);
				abyte[j++] = (byte) ((c & 0x3F) | 0x80);
				str += "%" + Integer.toHexString(abyte[j - 1]).substring(6);
			} else if (c < 0x200000) {
				abyte[j++] = (byte) (((c >> 18) & 0x07) | 0xF8);
				str += "%" + Integer.toHexString(abyte[j - 1]).substring(6);
				abyte[j++] = (byte) (((c >> 12) & 0x3F) | 0x80);
				str += "%" + Integer.toHexString(abyte[j - 1]).substring(6);
				abyte[j++] = (byte) (((c >> 6) & 0x3F) | 0x80);
				str += "%" + Integer.toHexString(abyte[j - 1]).substring(6);
				abyte[j++] = (byte) ((c & 0x3F) | 0x80);
				str += "%" + Integer.toHexString(abyte[j - 1]).substring(6);
			}
		}
		return str;
	}

	public static boolean isNew(Map<String,String> map) {
		return "isNew".equals(map.get("_rowcheck"));
	}

	public static double toDouble(Object str) {
		if (str == null) {
			return 0;
		}
		if (str instanceof Integer) {
			Integer n = (Integer) str;
			return n.doubleValue();
		} else if (str instanceof Long) {
			Long n = (Long) str;
			return n.doubleValue();
		} else if (str instanceof Double) {
			Double n = (Double) str;
			return n.doubleValue();
		} else if (str instanceof java.math.BigDecimal) {
			BigDecimal n = (BigDecimal) str;
			return n.doubleValue();
		}

		try {
			return Double.parseDouble(str.toString().trim());
		} catch (Exception e) {
			return 0;
		}
	}

	public static Double toDoubleObj(Object str) {
		if (str == null) {
			return new Double(0);
		}
		if (str instanceof Double) {
			return (Double) str;
		}
		return new Double(CM.toDouble(str));
	}

	public static Integer toIntegerObj(Object str) {
		if (str == null) {
			return new Integer(0);
		}
		if (str instanceof Integer) {
			return (Integer) str;
		}
		return new Integer(CM.toInt(str));
	}

	public static Long toLongObj(Object str) {
		if (str == null) {
			return new Long(0);
		}
		if (str instanceof Long) {
			return (Long) str;
		}
		return new Long(CM.toLong(str));
	}

	public static String java2sqlName(Object obj) {
		if (obj == null)
			return null;
		String s = obj.toString();
		StringBuffer sqlBuffer = new StringBuffer(s.length() * 2);
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z')
				sqlBuffer.append('_');
			sqlBuffer.append(s.charAt(i));
		}
		return sqlBuffer.toString().toLowerCase();
	}

	public static String sql2JavaName(Object obj) {
		if (obj == null)
			return null;
		String s = obj.toString();
		StringBuffer strBuffer = new StringBuffer(s.length());
		boolean bf = false;
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) == '_') {
				bf = true;
				continue;
			}
			if (bf) {
				strBuffer.append(Character.toUpperCase(s.charAt(i)));
				bf = false;
			} else {
				strBuffer.append(Character.toLowerCase(s.charAt(i)));
			}
		}
		return strBuffer.toString();
	}

	public static String stringNoNull(Object obj) {
		if (obj == null)
			return "";
		return obj.toString();
	}

	public static String stringNoNull(Object obj, String defaultStr) {
		if (obj == null)
			return defaultStr;
		return obj.toString();
	}

	public static void main(String args[]) {

		System.out.println(CM.formatNumber(3, "000"));
	}

	public static String formatNumber(Object d) {
		if (d == null)
			return "0.00";
		String retStr = "0.00";
		try {
			DecimalFormat dFormat = new DecimalFormat("#0.00");
			if (d instanceof String) {
				retStr = dFormat.format(Double.valueOf(d.toString()));
			} else {
				retStr = dFormat.format(d);
			}
			return retStr;
		} catch (Exception e) {
			return retStr;
		}
	}

	public static String formatNumber(Object d, String format) {
		if (d == null)
			d = new Double(0);
		String retStr = "0";
		try {
			DecimalFormat dFormat = new DecimalFormat(format);
			if (d instanceof String) {
				retStr = dFormat.format(Double.valueOf(d.toString()));
			} else {
				retStr = dFormat.format(d);
			}
			return retStr;
		} catch (Exception e) {
			return retStr;
		}
	}

	public static String formatDate() {
		return CM.formatDate("yyyy-MM-dd");
	}

	public static String formatDate(Object o) {
		if (o instanceof String) {
			return CM.formatDate(new java.util.Date(), o.toString());
		} else {
			return CM.formatDate(o, "yyyy-MM-dd");
		}
	}

	public static String formatDate(Object o, String format) {
		if (o == null)
			return null;
		String retStr = "";
		try {
			java.util.Date dt = null;
			if (o instanceof java.util.Calendar) {
				java.util.Calendar cal = (java.util.Calendar) o;
				dt = cal.getTime();
			} else if (o instanceof java.util.Date) {
				dt = (java.util.Date) o;
			} else if (o instanceof String) {
				return o.toString();
			} else {
				System.out.println("formatDate:Error Object class="
						+ o.getClass());
				return "Error Object";
			}
			DateFormat dFormat = new SimpleDateFormat(format);
			retStr = dFormat.format(dt);
			return retStr;
		} catch (Exception e) {
			e.printStackTrace();
			return retStr;
		}
	}

	public static String formatNumber(double d) {
		String retStr = "0.00";
		try {
			DecimalFormat dFormat = new DecimalFormat("#0.00");
			retStr = dFormat.format(d);
			return retStr;
		} catch (Exception e) {
			e.printStackTrace();
			return retStr;
		}
	}

	public static String formatNumber(double d, String format) {
		String retStr = "0";
		try {
			DecimalFormat dFormat = new DecimalFormat(format);
			retStr = dFormat.format(d);
			return retStr;
		} catch (Exception e) {
			return retStr;
		}
	}

	public static java.util.Date toDate(Object strDate) {
		return CM.toDate(strDate, "yyyy-MM-dd");
	}

	public static java.util.Date toDate(Object strDate, String format) {
		if (strDate == null)
			return null;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(strDate.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void setDefault(Map<String,String> map, String key, String defaultValue) {
		Object obj = map.get(key);
		if (obj == null || obj.equals("")) {
			map.put(key, defaultValue);
		}
	}

	public static boolean stringIsNotEmpty(String str) {
		if (str != null && str.trim().length() > 0)
			return true;
		else
			return false;
	}

	public static boolean stringIsEmpty(String str) {
		if (str == null || str.trim().length() == 0)
			return true;
		else
			return false;
	}

	public static byte[] compress(byte[] content) throws IOException {
		if (content == null)
			return null;
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		ZipOutputStream zos = new ZipOutputStream(os);
		zos.putNextEntry(new ZipEntry("data"));
		zos.write(content);
		zos.close();
		os.close();
		return os.toByteArray();
	}

	public static byte[] decompress(byte[] data) throws IOException {
		if (data == null)
			return null;
		ByteArrayInputStream is = new ByteArrayInputStream(data);
		ZipInputStream zis = new ZipInputStream(is);
		ZipEntry ze = zis.getNextEntry();
		if (ze != null) {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte buf[] = new byte[1024];
			int len = 0;
			while ((len = zis.read(buf)) > 0) {
				bos.write(buf, 0, len);
			}
			zis.close();
			bos.close();
			return bos.toByteArray();
		} else {
			System.out.println("没有压缩数据............");
			throw new IOException("没有压缩数据............");
		}
	}

	public static String[] splitString(String str, boolean needNull) {
		return splitString(str, ",", needNull);
	}

	public static String[] splitString(String str, String delim,
			boolean needNull) {
		List<String> list = new ArrayList<String>();
		if (needNull) {
			String strTemp = str;
			while (true) {
				int n = strTemp.indexOf(delim);
				if (n < 0) {
					list.add(strTemp);
					break;
				}
				list.add(strTemp.substring(0, n));
				strTemp = strTemp.substring(n + delim.length());
			}
		} else {
			StringTokenizer st = new StringTokenizer(str, delim);
			while (st.hasMoreElements()) {
				list.add(st.nextToken());
			}
		}
		String[] retStr = new String[list.size()];
		for (int i = 0; i < list.size(); i++)
			retStr[i] = (String) list.get(i);
		return retStr;
	}

	public static boolean isBase64Cert(byte[] data) {
		if (data == null)
			return false;
		for (int i = 0; i < data.length; i++) {
			char c = (char) data[i];
			if (CM.base64.indexOf(c) < 0) {
				return false;
			}
		}
		return true;
	}
}
